package com.gesee.zyspps.utils;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Point;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.AMapUtils;
import com.amap.api.maps2d.model.LatLng;
import com.gesee.zyspps.ZSApplication;
import com.gesee.zyspps.db.CmsLine;
import com.gesee.zyspps.db.CmsLineDao.Properties;
import com.gesee.zyspps.db.CmsPoints;
import com.gesee.zyspps.db.CmsSpecialLine;
import com.gesee.zyspps.model.ZysppsParams;

public class PointLineUtil
{
    private static double BEISHU1 = 30.0;
    private static double BEISHU4 = 10.0;
    private static double BEISHU8 = 5.0;
    /**
     * 
     * @param latlng
     * @param context
     * @return 返回可用点的名字。
     */
    public static String getMapName(LatLng latlng,Context context , int beishu)
    {
        List<Float> distanceList = new ArrayList<Float>();
        List<Float> distanceYesList = new ArrayList<Float>();
        List<CmsPoints> pointsList = ZSApplication.getDaoSession(ZSApplication.getInstance().getConfig("newDBName")).getCmsPointsDao().loadAll();
        for (int i = 0; i < pointsList.size(); i++)
        {
            double lat = pointsList.get(i).getD_X();
            double lng = pointsList.get(i).getD_Y();
            LatLng latLng = new LatLng(lat, lng);
            float dis = AMapUtils.calculateLineDistance(latlng, latLng);
            distanceList.add(dis);
            if (beishu == 1)
            {
                if (dis < BEISHU1)
                {
                    distanceYesList.add(dis);
                }
            }
            if (beishu == 4)
            {
                if (dis < BEISHU4)
                {
                    distanceYesList.add(dis);
                }
            }
            if (beishu == 8)
            {
                if (dis < BEISHU8)
                {
                    distanceYesList.add(dis);
                }
            }
            
        }
        
        if (distanceYesList.size() == 0)
        {
            return "isNull";
        }
        
        int distanceIndex = QuickSortUtil.quickSort(distanceList);

        return pointsList.get(distanceIndex).getExpNO();
    }
    
    public static LatLng getMapLatLng(LatLng latlng, int beishu)
    {
        LatLng stateLatLng = new LatLng(0.0, 0.0);
        List<Float> distanceList = new ArrayList<Float>();
        List<Float> distanceYesList = new ArrayList<Float>();
        List<CmsPoints> pointsList = ZSApplication.getDaoSession(ZSApplication.getInstance().getConfig("newDBName")).getCmsPointsDao().loadAll();
        for (int i = 0; i < pointsList.size(); i++)
        {
            double lat = pointsList.get(i).getD_X();
            double lng = pointsList.get(i).getD_Y();
            LatLng latLng = new LatLng(lat, lng);
            float dis = AMapUtils.calculateLineDistance(latlng, latLng);
            distanceList.add(dis);
            if (beishu == 1)
            {
                if (dis < BEISHU1)
                {
                    distanceYesList.add(dis);
                }
            }
            if (beishu == 4)
            {
                if (dis < BEISHU4)
                {
                    distanceYesList.add(dis);
                }
            }
            if (beishu == 8)
            {
                if (dis < BEISHU8)
                {
                    distanceYesList.add(dis);
                }
            }
        }
        
        if (distanceYesList.size() == 0)
        {
            return stateLatLng;
        }
        
        int distanceIndex = QuickSortUtil.quickSort(distanceList);
        double latitude = pointsList.get(distanceIndex).getD_X();
        double longitude = pointsList.get(distanceIndex).getD_Y();
        LatLng newLatLng = new LatLng(latitude, longitude);
        return newLatLng;
    }
    
    public  static boolean getLineNameIsIdentical(String mapNameStart,String mapNameStop)
    {
        boolean isIdentical = false;
        List<CmsLine> linesOneList = ZSApplication.getDaoSession(ZSApplication.getInstance().getConfig("newDBName")).getCmsLineDao()
                .queryBuilder().where(Properties.S_Point.eq(mapNameStart), Properties.E_Point.eq(mapNameStop)).list();
        
        List<CmsLine> linesTwoList = ZSApplication.getDaoSession(ZSApplication.getInstance().getConfig("newDBName")).getCmsLineDao()
                .queryBuilder().where(Properties.S_Point.eq(mapNameStop), Properties.E_Point.eq(mapNameStart)).list();
                
        if (linesOneList.size() > 0 || linesTwoList.size() > 0)
        {
            isIdentical = true;
        }
        return isIdentical;
    }
    
    /**
     * 
    * @Title: getLineIsOnePoint
    * @Description: TODO(判断射线)
    * @author fujianxiong
    * @return
    * @throws
     */
    public static String getLineIsOnePoint(LatLng latLng, int beishu)
    {
        String str = "isNull";
        List<Float> distanceList = new ArrayList<Float>();
        List<Float> distanceYesList = new ArrayList<Float>();
        List<CmsSpecialLine> specialLineList = ZSApplication.getDaoSession(ZSApplication.getInstance().getConfig("newDBName")).getCmsSpecialLineDao().loadAll();
        if (specialLineList.size() > 0)
        {
            for (int i = 0; i < specialLineList.size(); i++)
            {
                double specialLatitude = specialLineList.get(i).getStop_X();
                double specialLongitude = specialLineList.get(i).getStop_Y();
                LatLng specialLatLng = new LatLng(specialLatitude, specialLongitude);
                float dis = AMapUtils.calculateLineDistance(latLng, specialLatLng);
                distanceList.add(dis);
                
                if (beishu == 1)
                {
                    if (dis < BEISHU1)
                    {
                        distanceYesList.add(dis);
                    }
                }
                if (beishu == 4)
                {
                    if (dis < BEISHU4)
                    {
                        distanceYesList.add(dis);
                    }
                }
                if (beishu == 8)
                {
                    if (dis < BEISHU8)
                    {
                        distanceYesList.add(dis);
                    }
                }
                
                if (distanceYesList.size() == 0)
                {
                    return "isNull";
                }
            }
        }
        
        if (distanceList.size() > 0)
        {
            int distanceIndex = QuickSortUtil.quickSort(distanceList);
            str = specialLineList.get(distanceIndex).getExpNO()
                    +","+specialLineList.get(distanceIndex).getStart_X()
                    +","+specialLineList.get(distanceIndex).getStart_Y()
                    +","+specialLineList.get(distanceIndex).getStop_X()
                    +","+specialLineList.get(distanceIndex).getStop_Y();
            LogUtil.e("工具类获取到的："+specialLineList.get(distanceIndex).getExpNO()
                    +","+specialLineList.get(distanceIndex).getStart_X()
                    +","+specialLineList.get(distanceIndex).getStart_Y()
                    +","+specialLineList.get(distanceIndex).getStop_X()
                    +","+specialLineList.get(distanceIndex).getStop_Y());
        }

        return str;
    }
    
    /**
     * 点在线上。
     * @param latLng
     * @return
     */
    public static String getPointIntoLine(LatLng latLng,AMap aMap)
    {
        //将高德经纬度转成屏幕坐标。
        Point point = aMap.getProjection().toScreenLocation(latLng);
        int x = point.x;
        int y = point.y;
        
        String pointIntoLine = "isNull";
        
        List<CmsSpecialLine> specialLineList = ZSApplication.getDaoSession(ZSApplication.getInstance().getConfig("newDBName")).getCmsSpecialLineDao().loadAll();
        if (specialLineList.size() > 0)
        {
            for (int i = 0; i < specialLineList.size(); i++)
            {
                double specialStartLatitude = specialLineList.get(i).getStart_X();
                double specialStartLongitude = specialLineList.get(i).getStart_Y();
                double specialStopLatitude = specialLineList.get(i).getStop_X();
                double specialStopLongitude = specialLineList.get(i).getStop_Y();
                Point point1 = aMap.getProjection().toScreenLocation(new LatLng(specialStartLatitude, specialStartLongitude));
                Point point2 = aMap.getProjection().toScreenLocation(new LatLng(specialStopLatitude, specialStopLongitude));
                int x1 = point1.x;
                int y1 = point1.y;
                int x2 = point2.x;
                int y2 = point2.y;
                boolean isTrue = pointToLine(x1, y1, x2, y2, x, y);
                if (isTrue)
                {
                    pointIntoLine = specialStartLatitude+ "," + specialStartLongitude +","+ specialStopLatitude +","+ specialStopLongitude;
                    break;
                }
            }
        }
        
        List<CmsLine> lineList = ZSApplication.getDaoSession(ZSApplication.getInstance().getConfig("newDBName")).getCmsLineDao().loadAll();
        if (lineList.size() > 0)
        {
            for (int i = 0; i < lineList.size(); i++)
            {
                String startPoint = lineList.get(i).getS_Point();
                String stopPoint = lineList.get(i).getE_Point();
                
                CmsPoints points1 = ZSApplication.getDaoSession(ZSApplication.getInstance().getConfig("newDBName")).getCmsPointsDao().queryBuilder()
                        .where(com.gesee.zyspps.db.CmsPointsDao.Properties.ExpNO.eq(startPoint)).build().list().get(0);
                
                CmsPoints points2 = ZSApplication.getDaoSession(ZSApplication.getInstance().getConfig("newDBName")).getCmsPointsDao().queryBuilder()
                        .where(com.gesee.zyspps.db.CmsPointsDao.Properties.ExpNO.eq(stopPoint)).build().list().get(0);
                
                double specialStartLatitude = points1.getD_X();
                double specialStartLongitude = points1.getD_Y();
                double specialStopLatitude = points2.getD_X();
                double specialStopLongitude = points2.getD_Y();
                
                Point point1 = aMap.getProjection().toScreenLocation(new LatLng(specialStartLatitude, specialStartLongitude));
                Point point2 = aMap.getProjection().toScreenLocation(new LatLng(specialStopLatitude, specialStopLongitude));
                int x1 = point1.x;
                int y1 = point1.y;
                int x2 = point2.x;
                int y2 = point2.y;
                boolean isTrue = pointToLine(x1, y1, x2, y2, x, y);
                if (isTrue)
                {
                    pointIntoLine = specialStartLatitude+ "," + specialStartLongitude +","+ specialStopLatitude +","+ specialStopLongitude;
                    break;
                }
            }
        }
        
        
        return pointIntoLine;
    }
    
    public static String getUpdatePointIntoLine(LatLng latLng,AMap aMap)
    {
        //将高德经纬度转成屏幕坐标。
        Point point = aMap.getProjection().toScreenLocation(latLng);
        int x = point.x;
        int y = point.y;
        
        String pointIntoLine = "isNull";
        
        List<CmsLine> lineList = ZSApplication.getDaoSession(ZSApplication.getInstance().getConfig("newDBName")).getCmsLineDao().loadAll();
        if (lineList.size() > 0)
        {
            for (int i = 0; i < lineList.size(); i++)
            {
                String startPoint = lineList.get(i).getS_Point();
                String stopPoint = lineList.get(i).getE_Point();
                
                CmsPoints points1 = ZSApplication.getDaoSession(ZSApplication.getInstance().getConfig("newDBName")).getCmsPointsDao().queryBuilder()
                        .where(com.gesee.zyspps.db.CmsPointsDao.Properties.ExpNO.eq(startPoint)).build().list().get(0);
                
                CmsPoints points2 = ZSApplication.getDaoSession(ZSApplication.getInstance().getConfig("newDBName")).getCmsPointsDao().queryBuilder()
                        .where(com.gesee.zyspps.db.CmsPointsDao.Properties.ExpNO.eq(stopPoint)).build().list().get(0);
                
                double specialStartLatitude = points1.getD_X();
                double specialStartLongitude = points1.getD_Y();
                double specialStopLatitude = points2.getD_X();
                double specialStopLongitude = points2.getD_Y();
                
                Point point1 = aMap.getProjection().toScreenLocation(new LatLng(specialStartLatitude, specialStartLongitude));
                Point point2 = aMap.getProjection().toScreenLocation(new LatLng(specialStopLatitude, specialStopLongitude));
                int x1 = point1.x;
                int y1 = point1.y;
                int x2 = point2.x;
                int y2 = point2.y;
                boolean isTrue = pointToLine(x1, y1, x2, y2, x, y);
                if (isTrue)
                {
                    pointIntoLine = specialStartLatitude+ "," + specialStartLongitude +","+ specialStopLatitude +","+ specialStopLongitude;
                    break;
                }
            }
        }
        
        return pointIntoLine;
    }
    
    /**
     * 判断该经纬度是否是已有点
     * @return
     */
    public static boolean getLatLngIsPoint(double latitude,double longitude)
    {
        boolean isTure = false;
        List<CmsPoints> pointsList = ZSApplication.getDaoSession(ZSApplication.getInstance().getConfig("newDBName"))
                .getCmsPointsDao().queryBuilder().where(
                        com.gesee.zyspps.db.CmsPointsDao.Properties.D_X.eq(latitude),
                        com.gesee.zyspps.db.CmsPointsDao.Properties.D_Y.eq(longitude)).build().list();
        if (pointsList.size() > 0)
        {
            isTure = true;
        }
        return isTure;
    }
    
    /**
     * 
    * @Title: pointToLine
    * @Description: TODO(计算点是否在线上)
    * @author fujianxiong
    * @return
    * @throws
     */
    private static boolean pointToLine(int x1, int y1, int x2, int y2, int x0,  int y0)
    {
        boolean space = false;  
        double a, b, c;  
        a = lineSpace(x1, y1, x2, y2);// 线段的长度  
        b = lineSpace(x1, y1, x0, y0);// (x1,y1)到点的距离  
        c = lineSpace(x2, y2, x0, y0);// (x2,y2)到点的距离  
        if (c+b >= a  && c+b-a <= 2) {//点在线段上  
            space = true;
        }  
        return space; 
    }
    
 // 计算两点之间的距离  
    
    private static double lineSpace(int x1, int y1, int x2, int y2) 
    {  
        double lineLength = 0;  
        lineLength = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));  
        return lineLength;  
    }  
}